package com.cqcj.wms.controller;

import com.cqcj.wms.entity.StoreMain;
import com.cqcj.wms.entity.StoreSub;
import com.cqcj.wms.query.QueryPageParam;
import com.cqcj.wms.query.QueryResult;
import com.cqcj.wms.query.UpdateResult;
import com.cqcj.wms.service.IStoreMainService;
import com.cqcj.wms.service.IStoreSubService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author：zhouxiaobao
 * @name：StoreController
 * @Date：2024/10/17 14:42
 */
@RestController
@RequestMapping("store")
public class StoreController {

    @Autowired
    private IStoreMainService storeMainService;

    @Autowired
    private IStoreSubService storeSubService;

    /**
     * 入库供应商列表
     * @return
     */
    @GetMapping("storeMainList")
    public UpdateResult storeMainList(){
        return UpdateResult.suc(storeMainService.list());
    }

    /**
     * 查询入库供应商分页数据
     * @param queryPageParam
     * @return
     */
    @PostMapping("pageMainList")
    public QueryResult pageMainList(@RequestBody QueryPageParam queryPageParam){

        HashMap param = queryPageParam.getParam();
        String vendor = (String) param.get("vendor");

        StoreMain storeMain = new StoreMain();
        storeMain.setVendor(vendor);

        PageInfo<StoreMain> pageInfo =
                storeMainService.pageByConditionList(
                        queryPageParam.getPageNum(),
                        queryPageParam.getPageSize(), storeMain);
        return QueryResult.suc(pageInfo.getList(),pageInfo.getTotal(), pageInfo.getPageSize());
    }

    /**
     *查询入库记录分页数据
     * @param queryPageParam
     * @return
     */
    @PostMapping("pageSubList")
    public QueryResult pageSubList(@RequestBody QueryPageParam queryPageParam){
        HashMap param = queryPageParam.getParam();
        String vendor = (String) param.get("vendor");
        String storageId =  param.get("storageId").toString();
        String variety = param.get("variety").toString();
        StoreSub storeSub = new StoreSub();
        //根据供应商id查询
        if(!storageId.equals("")){
            storeSub.setStorageId(Integer.parseInt(storageId));
        }
        StoreMain storeMain = new StoreMain();
        storeSub.setStoreMain(storeMain);
        //根据供应商名称查询
        if(vendor != null && !vendor.trim().equals("")){
            storeSub.getStoreMain().setVendor(vendor);
        }

        //根据入库商品品种查询
        if(variety != null && !variety.trim().equals("")){
            storeSub.setVariety(variety);
        }

        PageInfo<StoreSub> pageInfo = storeSubService.listByConditions(queryPageParam.getPageNum(), queryPageParam.getPageSize(),storeSub);
        return QueryResult.suc(pageInfo.getList(),pageInfo.getTotal(), pageInfo.getPageSize());
    }

    /**
     * 添加或修改入库主列表信息
     * @param storeMain
     * @return
     */
    @PostMapping("saveOrUpdateMain")
    public UpdateResult saveOrUpdateMain(@RequestBody StoreMain storeMain){
        //供应商名称非空验证
        String vendor = storeMain.getVendor().trim();
        //封装结果集
        //判断name是否为空
        if(StringUtils.isEmpty(vendor)){
            return UpdateResult.fail("供应商名称不能为空！");
        }

        //添加操作
        int status = 0;
        status = storeMain.getId() == null ? storeMainService.insert(storeMain) : storeMainService.update(storeMain);
        if(status == 0){//添加失败
            return UpdateResult.fail();
        }
        return UpdateResult.suc();
    }

    /**
     *通过id删除入库信息的方法
     * @param id
     * @return
     */
    @PostMapping("deleteMain")
    public UpdateResult deleteMain(@RequestParam int id){
        //删除之前判断该供应商下是否还有入库记录存在
        StoreMain storeMain = storeMainService.getEntityById(id);
        List<StoreSub> lists = new ArrayList<>();
        if(storeMain != null){
            lists = storeSubService.lists(id);
        }
        // 若存在，则不删除
        if(lists.size() > 0){
            return UpdateResult.fail("当前供应商下还有入库记录,请勿删除");
        }
        int result = storeMainService.delete(id);
        if(result == 0){
            return UpdateResult.fail("删除失败");
        }
        return UpdateResult.suc();
    }


    /**
     * 添加或修改入库记录信息
     * @param storeSub
     * @return
     */
    @PostMapping("saveOrUpdateSub")
    public UpdateResult saveOrUpdateSub(@RequestBody StoreSub storeSub){

        int num = storeSub.getNum();
        double price = storeSub.getPrice();
        double amount = num * price;

        storeSub.setAmount(amount);
        //添加操作
        int status = 0;
        status = storeSub.getId() == null ? storeSubService.insert(storeSub) : storeSubService.update(storeSub);
        if(status == 0){//添加失败
            return UpdateResult.fail();
        }
        return UpdateResult.suc();
    }

    /**
     * 根据id删除出入库数据
     * @param id
     * @return
     */
    @PostMapping("deleteSub")
    public UpdateResult deleteSub(@RequestParam int id){
        //如果存在，正常删除，删除成功与否，将信息返回给客户端
        int result = storeSubService.delete(id);
        if(result == 0){
            return UpdateResult.fail("删除失败");
        }
        return UpdateResult.suc();
    }
}
